package com.leo;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName TreeTableViewDemo
 * @Description
 * @Author wangss
 * @date 2020.06.17 22:11
 * @Version 1.0
 */
public class TreeTableViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #f4fffe");

        Data data1 = new Data("A", 12, true);
        Data data2 = new Data("B", 11, true);
        Data data3 = new Data("C", 10, true);
        Data data4 = new Data("D", 14, true);

        TreeTableView<Data> treeTableView = new TreeTableView<>();

        TreeItem<Data> root = new TreeItem<>(data1);
        TreeItem<Data> t2 = new TreeItem<>(data2);
        TreeItem<Data> t3 = new TreeItem<>(data3);
        TreeItem<Data> t4 = new TreeItem<>(data4);
        root.getChildren().addAll(t2, t3, t4);
        treeTableView.setRoot(root);

        root.setExpanded(true);

        TreeTableColumn<Data, String> name = new TreeTableColumn<>("姓名");
        name.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));

        TreeTableColumn<Data, Integer> age = new TreeTableColumn<>("年龄");
        age.setCellValueFactory(new TreeItemPropertyValueFactory<>("age"));

        TreeTableColumn<Data, Boolean> flag = new TreeTableColumn<>("是否");
        flag.setCellValueFactory(new TreeItemPropertyValueFactory<>("flag"));

        treeTableView.getColumns().addAll(name, age, flag);
        treeTableView.setPrefWidth(300d);
        treeTableView.setPrefHeight(300d);
        treeTableView.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

        Button button = new Button("bu");
        AnchorPane.setTopAnchor(treeTableView, 150d);
        AnchorPane.setLeftAnchor(treeTableView, 150d);
        an.getChildren().addAll(button, treeTableView);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeTableView");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();

        button.setOnAction(event -> {
            data1.setName("wangss");
        });

//        treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        treeTableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Data>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<Data>> observable, TreeItem<Data> oldValue, TreeItem<Data> newValue) {
                System.out.println(newValue.getValue().getName());
            }
        });

        treeTableView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

            }
        });

        treeTableView.getSelectionModel().getSelectedCells().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                ObservableList<TreeTablePosition<Data, ?>> list = (ObservableList<TreeTablePosition<Data, ?>>) observable;
                for (int i = 0; i < list.size(); i++) {
                    TreeTablePosition<Data, ?> dataTreeTablePosition = list.get(i);
                    Object cellData = dataTreeTablePosition.getTableColumn().getCellData(dataTreeTablePosition.getRow());
                    System.out.println("行 = " + dataTreeTablePosition.getRow() + " 列 = " + dataTreeTablePosition.getColumn() + " 数据 = " + cellData.toString());
                }
            }
        });

        treeTableView.getSelectionModel().getSelectedCells().addListener(new ListChangeListener<TreeTablePosition<Data, ?>>() {
            @Override
            public void onChanged(Change<? extends TreeTablePosition<Data, ?>> c) {
                ObservableList<? extends TreeTablePosition<Data, ?>> list = c.getList();
                for (int i = 0; i < list.size(); i++) {
                    TreeTablePosition<Data, ?> treeTablePosition = list.get(i);
                    Object cellData = treeTablePosition.getTableColumn().getCellData(treeTablePosition.getRow());
                    System.out.println("行 = " + treeTablePosition.getRow() + " 列 = " + treeTablePosition.getColumn() + " 数据 = " + cellData.toString());
                }
            }
        });
    }
}
